<template>
  <div>
    <el-dialog :title="type + '工艺设计'" :visible.sync="visible" width="1200px">
      <el-row class="mb20">
        <el-col :span="6">
          <el-button type="primary" icon="el-icon-plus" @click="createProcess" v-if="$checkAuth('修改工艺路线')">新建工艺版本</el-button>
        </el-col>
        <el-col :span="6">
          <label for="drawingNumber">图号</label>
          <div class="inline-label ml10" name="drawingNumber">
            {{ basics.drawingNumber || '无' }}
          </div>
        </el-col>
        <el-col :span="6">
          <label for="productName">品名</label>
          <div name="productName" class="inline-label ml10">
            {{ basics.productName || '无' }}
          </div>
        </el-col>
        <el-col :span="6">
          <label for="number">数量</label>
          <div name="number" class="inline-label ml10">{{ basics.number || '0' }}</div>
        </el-col>
      </el-row>
      <div class="flex justify-space-between">
        <div class="w-50percent border-x mr10">
          <el-collapse v-model="activeVersion" @change="handleChange" accordion v-if="versionList.length > 0">
            <template v-for="(item, index) in versionList">
              <el-collapse-item :name="index" :key="index" class="p10-x">
                <template slot="title">
                  <div class="justify-space-between flex w-100percent">
                    <span>版本: {{ item.versionName }}</span>
                    <i class="iconfont icon-xiugai1 fontSize14 pr15" @click.stop="editProcess(index)" v-if="$checkAuth('修改工艺路线')"></i>
                  </div>
                </template>
                <el-table :data="item.processList" row-key="sid" :tree-props="{ children: 'equipmentList', hasChildren: 'hasChildren' }" default-expand-all>
                  <el-table-column prop="sequenceNumber" label="序号" width="70" align="right"> </el-table-column>
                  <el-table-column prop="processName" label="工序" width="100"> </el-table-column>
                  <el-table-column prop="single" label="单机" width="60">
                    <template slot-scope="scope">
                      <div v-if="scope.row.department == '加工'">{{ scope.row.single ? '单机' : '双机' }}</div>
                      <!--      <el-input v-model="scope.row.remark"></el-input>-->
                    </template>
                  </el-table-column>
                  <el-table-column prop="department" label="部门" align="center" width="100"></el-table-column>
                  <el-table-column prop="equipment" label="设备/供应商" align="center" width="175"></el-table-column>
                  <el-table-column prop="debugHour" label="定额工时" align="center" width="125"></el-table-column>
                  <el-table-column prop="quotaHour" label="调试工时" align="center" min-width="110"></el-table-column>
                </el-table>
              </el-collapse-item>
            </template>
          </el-collapse>
          <div v-else>
            <no-data placeholder="这里还没有数据哦，快去新建工艺版本吧~"></no-data>
          </div>
        </div>
        <div class="flex justify-space-around flex__item">
          <div class="flex__item border">
            <el-tag class="w-100percent">未选工艺版本</el-tag>
            <div class="flex-column mb10 mt10">
              <template v-for="(item, index) in noChooseVersionList">
                <div :key="index" class="border p-10 cursor-pointer" @click="curChooseVersionId = item.versionId" :class="curChooseVersionId == item.versionId ? 'choose-color' : ''">
                  <span>{{ item.versionName }}</span>
                  <span class="fr mr5">{{ item.versionState }}</span>
                </div>
              </template>
            </div>
          </div>
          <div class="flex-column justify-content-center ml10 mr10">
            <i class="el-icon-d-arrow-right mb10 bold" @click="gotoRight"></i>
            <i class="el-icon-d-arrow-left bold" @click="gotoLeft"></i>
          </div>
          <div class="flex__item border">
            <el-tag type="success" class="w-100percent">已选工艺版本</el-tag>
            <div class="flex-column mb10 mt10">
              <template v-for="(item, index) in chooseVersionList">
                <div :key="index" class="border p-10 cursor-pointer" @click="curChooseVersionId = item.versionId" :class="curChooseVersionId == item.versionId ? 'choose-color' : ''">
                  <span>{{ item.versionName }}</span>
                  <span class="fr mr5">{{ item.versionState }}</span>
                </div>
              </template>
            </div>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onConfirm">确认设计</el-button>
        <el-button @click="onCancel">取 消</el-button>
      </span>
    </el-dialog>
    <add-dialog :isHaveDrawing="true" ref="addDialog" @update="getProcessList()"></add-dialog>
    <edit-dialog ref="editDialog" @update="getProcessList()"></edit-dialog>
  </div>
</template>

<script>
import { getProcessDetail, designProcess } from '@/api/process'
import { Notification } from 'element-ui'
import AddDialog from './addProcessDialog.vue'
import EditDialog from './addProcessDetailDialog.vue'
import _ from 'lodash'
export default {
  data() {
    return {
      visible: false,
      basics: {
        drawingNumber: '1',
        productName: '2',
        number: '3'
      },
      versionList: [],
      activeVersion: null,
      productId: null,
      chooseVersionList: [], // 已选工艺版本
      noChooseVersionList: [], // 未选工艺版本
      curChooseVersionId: null,
      originChooseId: [], // 后端的初始已选工艺版本
      type: '' // "产品通知" "工装品" "返修品"
    }
  },
  components: {
    AddDialog,
    EditDialog
  },
  created() {},
  methods: {
    async open(data = {}, productId, type) {
      // 获取指定产品的多个工艺版本
      // getProcessDetail({producingId: productId});
      this.basics = { ...data }
      this.productId = productId
      this.getProcessList()
      this.visible = true
      this.type = type
    },
    async getProcessList() {
      let res = await getProcessDetail({ producingId: this.productId })
      if (res) {
        this.versionList = res.data
        // 设置row-key
        this.versionList = this.versionList.map(vItem => {
          vItem.processList = vItem.processList.map((pItem, pIndex) => {
            this.$set(pItem, 'sid', 'a' + pIndex)
            if (pItem.equipmentList) {
              pItem.equipmentList = pItem.equipmentList.map((eItem, eIndex) => {
                this.$set(eItem, 'sid', 'b' + pIndex + eIndex)
                return eItem
              })
            }
            return pItem
          })
          return vItem
        })
        this.chooseVersionList = this.versionList.filter(item => {
          return item.versionState
        })
        this.originChooseId = this.chooseVersionList.map(item => {
          return item.versionId
        })
        this.noChooseVersionList = this.versionList.filter(item => {
          return !item.versionState
        })
      }
    },
    createProcess() {
      this.$refs.addDialog.open(this.basics.drawingNumber)
    },
    async editProcess(index) {
      let process = this.versionList[index]
      let getdata = await this.$store.dispatch('TwoProcess/PREDATA_VERSION', { versionId: process.versionId })
      let getversion = await this.$store.dispatch('TwoProcess/GET_VERSION', { versionId: process.versionId })
      if (getdata && getversion) {
        this.$refs.editDialog.openEdit(process.versionId, process.versionName, process.drawingNumber, process.productName)
      }
    },
    gotoLeft() {
      let version = this.chooseVersionList.find(item => {
        return item.versionId == this.curChooseVersionId
      })
      let Index = this.chooseVersionList.findIndex(item => {
        return item.versionId == this.curChooseVersionId
      })
      if (version) {
        if ([null, '待审核', '审核未通过'].includes(version.versionState)) {
          this.noChooseVersionList.push(version)
          this.chooseVersionList.splice(Index, 1)
        } else {
          this.$message('只能移除[待审核][审核未通过]的工艺版本')
        }
      } else {
        this.$message('请先选择工艺版本')
      }
    },
    gotoRight() {
      let version = this.noChooseVersionList.find(item => {
        return item.versionId == this.curChooseVersionId
      })
      let Index = this.noChooseVersionList.findIndex(item => {
        return item.versionId == this.curChooseVersionId
      })
      if (version) {
        this.chooseVersionList.push(version)
        this.noChooseVersionList.splice(Index, 1)
      }
    },
    handleChange() {},
    onCancel() {
      this.curChooseVersionId = null
      this.visible = false
    },
    async onConfirm() {
      let ids = this.chooseVersionList.map(item => {
        return item.versionId
      })
      // 确认被设计的工艺版本不为空
      if (!_.isEmpty(ids)) {
        let res = await designProcess({ producingId: this.productId, versionIdList: ids }).catch(err => {
          this.$notify({
            title: '错误提醒',
            message: err
          })
        })
        if (res) {
          this.$emit('update', this.type) // 提示父组件要更新数据
          this.curChooseVersionId = null
          this.visible = false
        }
      } else {
        this.curChooseVersionId = null
        this.visible = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog {
  min-height: 300px;
}
.choose-color {
  background-color: #fb8989;
}
</style>
